package com.fw.ui.view.settings.usercontrol.bouser.event;

import static org.springframework.beans.factory.config.BeanDefinition.SCOPE_PROTOTYPE;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.fw.service.bean.SearchCriteriaBean;
import com.fw.service.bean.SearchResultBean;
import com.fw.service.bouser.bean.BOUserBean;
import com.fw.service.bouser.bean.SearchBOUserBean;
import com.fw.ui.ApplicationUI;
import com.fw.ui.bao.BOUserBAO;
import com.fw.ui.bao.MessageBAO;
import com.fw.ui.bao.PropertyBAO;
import com.fw.ui.util.SessionInfoUtils;
import com.fw.ui.util.VaadinApplicationObjectSupport;
import com.fw.ui.view.common.SortingTablePanel;
import com.fw.ui.view.common.TablePanel;
import com.fw.ui.view.event.AbstractButtonClickListener;
import com.fw.ui.view.settings.usercontrol.bouser.SearchBOUserView;
import com.vaadin.data.util.BeanItem;
import com.vaadin.ui.Button.ClickEvent;

@Component
@Scope(SCOPE_PROTOTYPE)
public class SearchBOUserSearchButtonClickListener extends AbstractButtonClickListener {

    private static final long serialVersionUID = 1L;

    @Autowired
    protected transient VaadinApplicationObjectSupport app;

    @Autowired
    private BOUserBAO boUserBAO;
    @Autowired
    private MessageBAO messageBAO;
    @Autowired
    private PropertyBAO propertyBAO;
    @Autowired
    private SessionInfoUtils sessionInfoUtils;

    @SuppressWarnings("rawtypes")
    @Override
    public Map<String, Object> click(ClickEvent event) throws Exception {
	SearchBOUserView searchBOUserView = (SearchBOUserView) this.getBasedComponent();

	TablePanel tablePanel = searchBOUserView.getTablePanel();
	SortingTablePanel sortingTablePanel = tablePanel.getSortingTablePanel();

	Integer size = tablePanel.getManualPageSize();
	int page = 1;

	String sort1 = (String) sortingTablePanel.getCbbSelectSort1().getValue();
	String sort2 = (String) sortingTablePanel.getCbbSelectSort2().getValue();
	String sort3 = (String) sortingTablePanel.getCbbSelectSort3().getValue();
	String sortType1 = (String) sortingTablePanel.getOpgSortingType1().getValue();
	String sortType2 = (String) sortingTablePanel.getOpgSortingType2().getValue();
	String sortType3 = (String) sortingTablePanel.getOpgSortingType3().getValue();

	Map<String, String> sortMap = new LinkedHashMap<String, String>();
	if (sort1 != null) {
	    sortMap.put(sort1, sortType1);
	}
	if (sort2 != null && !sortMap.containsKey(sort2)) {
	    sortMap.put(sort2, sortType2);
	}
	if (sort3 != null && !sortMap.containsKey(sort3)) {
	    sortMap.put(sort3, sortType3);
	}

	SearchBOUserBean searchBOUserBean = new SearchBOUserBean();

	searchBOUserBean.setUsername((String) searchBOUserView.getCbbUsername().getValue());

	searchBOUserBean.setEmail(((searchBOUserView.getTxtEmailAddress().getValue() == null || searchBOUserView.getTxtEmailAddress()
		.getValue().toString().trim().length() == 0) ? null : searchBOUserView.getTxtEmailAddress().getValue()));

	searchBOUserBean.setEmployeeID((String) searchBOUserView.getCbbEmployeeId().getValue());
	searchBOUserBean.setEmployeeName((String) searchBOUserView.getCbbEmployeeName().getValue());

	searchBOUserBean.setRole(((searchBOUserView.getCbbGroup().getValue() == null
		|| searchBOUserView.getCbbGroup().getValue().toString().trim().length() == 0 || searchBOUserView.getCbbGroup().getValue()
		.toString().trim().equals("0")) ? null : searchBOUserView.getCbbGroup().getValue().toString()));

	searchBOUserBean.setStatus(((searchBOUserView.getCbbStatus().getValue() == null
		|| searchBOUserView.getCbbStatus().getValue().toString().trim().length() == 0 || searchBOUserView.getCbbStatus().getValue()
		.toString().trim().equals("0")) ? null : searchBOUserView.getCbbStatus().getValue().toString()));

	searchBOUserBean.setCreateBy((String) searchBOUserView.getCbbCreateBy().getValue());
	searchBOUserBean.setLastUpdateBy((String) searchBOUserView.getCbbLastUpdateBy().getValue());
	searchBOUserBean.setExpiredDateFrom(searchBOUserView.getPdfExpireDateFrom().getValue());
	searchBOUserBean.setExpiredDateTo(searchBOUserView.getPdfExpireDateTo().getValue());
	searchBOUserBean.setCreateDateFrom(searchBOUserView.getPdfCreateDateFrom().getValue());
	searchBOUserBean.setCreateDateTo(searchBOUserView.getPdfCreateDateTo().getValue());
	searchBOUserBean.setLastUpdateDateFrom(searchBOUserView.getPdfLastUpdateDateFrom().getValue());
	searchBOUserBean.setLastUpdateDateTo(searchBOUserView.getPdfLastUpdateDateTo().getValue());

	ApplicationUI applicationUI = (ApplicationUI) searchBOUserView.getUI();
	sessionInfoUtils.setSessionInfoDetails(searchBOUserBean, applicationUI.getAuthenticationBean(), applicationUI.getLocale());

	SearchCriteriaBean<SearchBOUserBean> searchCriteriaBean = new SearchCriteriaBean<SearchBOUserBean>();
	if (!sortMap.isEmpty()) {
	    searchCriteriaBean.setSortingCriteria(sortMap);
	} else {
	    String sortingDefault = this.propertyBAO
		    .getPropertyValue("com.fw.ui.view.settings.usercontrol.bouser.SearchBOUserView.default.table.sorting");
	    searchCriteriaBean.setDirection1(SearchCriteriaBean.DIRECTION_DESC);
	    searchCriteriaBean.setOrder1(sortingDefault);
	}

	searchCriteriaBean.setCriteriaBean(searchBOUserBean);
	searchCriteriaBean.setPage(page);
	searchCriteriaBean.setSize(size);
	searchBOUserView.setSearchCriteriaBean(searchCriteriaBean);

	SearchResultBean<BOUserBean> searchResultBean = boUserBAO.selectBOUser(searchCriteriaBean);

	Integer totalPage = searchResultBean.getTotalPages();
	if (totalPage == null) {
	    totalPage = 1;
	}
	Long totalElement = searchResultBean.getTotalElements();
	if (totalElement == null) {
	    totalElement = 0l;
	}
	List<BeanItem> beanItems = searchBOUserView.generateTableItemBean(searchResultBean);

	searchBOUserView.getTablePanel().clearTableData();
	searchBOUserView.getTablePanel().setManualPageTable(1);
	searchBOUserView.getTablePanel().setTableBeanContainerDataTotalPage(beanItems, totalPage, totalElement);

	if (beanItems.isEmpty()) {
	    Map<String, Object> result = new HashMap<String, Object>();
	    result.put(MessageBAO.MESSAGE_INFO_TYPE, "message.search.not.found");
	    return result;
	}
	return null;
    }

    @Override
    public boolean validate(ClickEvent event) {
	return true;
    }

}
